﻿using Adnc.Application.Shared.Services;
using Adnc.Infra.Helper;
using Adnc.Infra.IRepositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using WYT.Usr.Application.Contracts.Dtos;
using WYT.Usr.Application.Contracts.Services;
using WYT.Usr.Repository.Entities;

namespace WYT.Usr.Application.Services
{
    /// <summary>
    /// 收货地址服务
    /// </summary>
    public class AddressAppService : AbstractAppService, IAddressAppService
    {
        private readonly IEfRepository<HarvestAddress> _addressRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="addressRepository"></param>
        public AddressAppService(IEfRepository<HarvestAddress> addressRepository)
        {
            _addressRepository = addressRepository;
        }

        /// <summary>
        /// 添加收货地址
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="input"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<AppSrvResult<long>> CreateAsync(long UserId, UserAddressCreationDto input)
        {
            var newAddress = Mapper.Map<HarvestAddress>(input);
            newAddress.Id = IdGenerater.GetNextId();
            newAddress.UsreId = UserId;

            if (input.IsDefault)
                await _addressRepository.UpdateRangeAsync(x => x.UsreId == UserId && x.IsDefault == true, c => new HarvestAddress { IsDefault = false });
            await _addressRepository.InsertAsync(newAddress);

            return AppSrvResult(newAddress.Id);
        }


        /// <summary>
        /// 删除收货地址
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="Id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<AppSrvResult<Boolean>> DeleteAsync(long UserId, long Id)
        {
            var address = await _addressRepository.FindAsync(Id);
            if (address == null || address.UsreId != UserId)
                return false;

            var result = await  _addressRepository.DeleteAsync(Id);
            return result > 0;
        }

        /// <summary>
        /// 更新地址
        /// </summary>
        /// <param name="UserId"></param>
        /// <param name="input"></param>
        /// <returns></returns>

        public async Task<AppSrvResult<Boolean>> UpdateAsync(long UserId, UserAddressUpdationDto input)
        {
            var address = await _addressRepository.FindAsync(input.Id);
            if (address == null || address.UsreId != UserId)
                return false;

            //如果将地址改成默认收货地址，需要查询是否有默认收货地址并修改其值
            if (!address.IsDefault && input.IsDefault)
                await _addressRepository.UpdateRangeAsync(x => x.UsreId == UserId && x.IsDefault == true, c =>  new HarvestAddress { IsDefault = false });

            address.ProvinceCode = input.ProvinceCode;
            address.ProvinceName = input.ProvinceName;
            address.CityCode = input.CityCode;
            address.CityName = input.CityName;
            address.AreaCode = input.AreaCode;
            address.AreaName = input.AreaName;
            address.StreetCode = input.StreetCode;
            address.StreetName = input.StreetName;
            address.Address = input.Address;
            address.Name = input.Name;
            address.Tele = input.Tele;
            address.IsDefault = input.IsDefault;
            address.IDCard = input.IDCard;
            address.Compellation = input.Compellation;

            var result = await _addressRepository.UpdateAsync(address, UpdatingProps<HarvestAddress>(
                        c => c.Name,
                        c => c.Tele,
                        c => c.Address,
                        c => c.ProvinceCode,
                        c => c.ProvinceName,
                        c => c.CityCode,
                        c => c.CityName,
                        c => c.AreaCode,
                        c => c.AreaName,
                        c => c.StreetCode,
                        c => c.StreetName,
                        c => c.IDCard,
                        c => c.Compellation,
                        c => c.IsDefault));
            return result > 0;
        }


        /// <summary>
        /// 获取地址列表
        /// </summary>
        /// <param name="UserId"></param>
        /// <returns></returns>
        public async Task<AppSrvResult<List<UserAddressDto>>> GetUserHarvestAddress(long UserId)
        {
            return await _addressRepository.Where(c => c.UsreId == UserId).OrderByDescending(c => c.IsDefault)
                .Select(c => new UserAddressDto {
                    Id = c.Id,
                    Name = c.Name,
                    Tele = c.Tele,
                    Address = c.Address,
                    ProvinceCode = c.ProvinceCode,
                    ProvinceName = c.ProvinceName,
                    CityCode = c.CityCode,
                    CityName = c.CityName,
                    AreaCode = c.AreaCode,
                    AreaName = c.AreaName,
                    StreetCode = c.StreetCode,
                    StreetName = c.StreetName,
                    IDCard = c.IDCard,
                    Compellation = c.Compellation,
                    IsDefault = c.IsDefault,
                }).ToListAsync();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public async Task<AppSrvResult<UserAddressDto>> SearchById(long Id)
        {
            var addr = await _addressRepository.FindAsync(Id);
            if (addr == null)
                return Problem(HttpStatusCode.NotFound, "");
            return Mapper.Map<UserAddressDto>(addr);
        }
    }
}
